import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class LettoreSpesa {
	private String foodName;
	private String description;
	private double amount;
	private String unitM;
	private String buyDay;
	private String expirationDay;
	private ArrayList<SubFood> foodList = new ArrayList<SubFood>();
	private String recipeName;
	private String recipeDescrition;
	private ArrayList<Ingredient> ingredientList = new ArrayList<Ingredient>();
	private ArrayList<Ricette> recipeList = new ArrayList<Ricette>();

	public void leggiFiles() {
		File dispensa = new File("dispensa.txt");
		if (dispensa.isFile()) {
			try {
				BufferedReader input = new BufferedReader(new FileReader(
						dispensa));
				String text;
				while ((text = input.readLine()) != null) {
					StringTokenizer st = new StringTokenizer(text, ",");
					while (st.hasMoreTokens()) {
						foodName = st.nextToken();
						description = st.nextToken();
						amount = Double.parseDouble(st.nextToken());
						unitM = st.nextToken();
						buyDay = st.nextToken();
						expirationDay = st.nextToken();
					}
					Food myfood = new SubFood(foodName, description, amount,
							unitM, expirationDay, buyDay);
					foodList.add((SubFood) myfood);
				}
				input.close();
			} catch (IOException ioException) {
				System.out.println("File non trovato");
			}
			File ricette = new File("ricettario.txt");
			if (ricette.isFile()) {

				try {
					String recipeIngredintName;
					double recipeIngredientAmount;
					boolean recipeIngedientOptional;
					String temp;
					BufferedReader input = new BufferedReader(new FileReader(
							ricette));
					String text;
					while ((text = input.readLine()) != null) {
						StringTokenizer st = new StringTokenizer(text, ",");
						while (st.hasMoreTokens()) {
							recipeName = st.nextToken();
							recipeDescrition = st.nextToken();
							st.countTokens();
							while (st.hasMoreTokens()) {
								temp = st.nextToken(" ");	
								recipeIngredintName=temp.replace(",","");//toglie la virgola di troppo lasciata da string tokenizer;
								recipeIngredientAmount = Double.parseDouble(st
										.nextToken(" "));
								temp=st.nextToken(",");
								if(temp.contains("0"))
									recipeIngedientOptional=false;
								else 
									recipeIngedientOptional= true;
								Ingredient myRecipeIngredient = new Ingredient(
										recipeIngredintName,
										recipeIngredientAmount,
										recipeIngedientOptional);
								ingredientList.add(myRecipeIngredient);
							}
							Ricette myRecipe = new Ricette(recipeName,
									recipeDescrition, ingredientList);
							recipeList.add(myRecipe);
						}
						System.out.println(ingredientList.toString());
						ingredientList.clear();
					}
					input.close();
				} catch (IOException ioException) {
					System.out.println("File non trovato");
				}
			}
		}
	}

	public String getFoodName() {
		return foodName;
	}

	public String getDescription() {
		return description;
	}

	public double getAmount() {
		return amount;
	}

	public String getUnitM() {
		return unitM;
	}

	public String getBuyDay() {
		return buyDay;
	}

	public String getExpirationDay() {
		return expirationDay;
	}

	public ArrayList<Ingredient> getIngredientsList() {
		return ingredientList;
	}

	public ArrayList<SubFood> getFoodList() {
		return foodList;
	}

	public ArrayList<Ricette> getRecipeList() {
		return recipeList;
	}
}
